//
//  EJSUpgradeVipViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2018/1/15.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "EJSUpgradeVipViewController.h"
#import "EJSUpgradeVipTableViewCell.h"
#import "EJSUpgradeVipDescriptionTableViewCell.h"
#import "EJSPayForVipViewController.h"

#import "EJSVipInfoRequest.h"

@interface EJSUpgradeVipViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) EJSVipInfoResponseModel *vipInfoResponseModel;
@property (strong, nonatomic) EJSVipInfoModel *selectVipModel;

@end

@implementation EJSUpgradeVipViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)configOwnViews{
    self.title = @"VIP";
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSUpgradeVipTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSUpgradeVipTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSUpgradeVipDescriptionTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSUpgradeVipDescriptionTableViewCell"];
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    self.tableView.tableFooterView = [UIView new];
}

- (void)loadData{
    EJSVipInfoRequest *req = [EJSVipInfoRequest new];
    req.page = @"0";
    req.pageLimit = @"1";
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        self.vipInfoResponseModel = response.data;
        [self.tableView reloadData];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

#pragma mark TableViewDataSoruce
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.vipInfoResponseModel.list.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row < self.vipInfoResponseModel.list.count){
        
        EJSVipInfoModel *model = self.vipInfoResponseModel.list[indexPath.row];
        EJSUpgradeVipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSUpgradeVipTableViewCell" forIndexPath:indexPath];
        NSString *vipContentStr = [NSString stringWithFormat:@"购买%@：€ %.2f",model.levelName,model.price.floatValue];
        if(model.available.integerValue == 0){
            vipContentStr = [vipContentStr stringByAppendingString:@"（不可用）"];
        }
        cell.vipTitleLabel.text = vipContentStr;
        cell.selectButton.tag = indexPath.row;
        cell.selectButton.selected = (self.selectVipModel == model);
        [cell.selectButton addTarget:self action:@selector(selectVipInfoAction:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else{
        EJSUpgradeVipDescriptionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSUpgradeVipDescriptionTableViewCell" forIndexPath:indexPath];
        [cell setIntro:self.vipInfoResponseModel.intro];
        return cell;
    }
}

- (void)selectVipInfoAction:(UIButton *)button{
    NSInteger index = button.tag;
    if(index < self.vipInfoResponseModel.list.count){
        
        EJSVipInfoModel *vipInfo = self.vipInfoResponseModel.list[index];
        if(vipInfo.available.integerValue == 0){
            
            [[HUDHelper sharedInstance] tipMessage:@"该VIP类型不可用"];
            return;
        }
        self.selectVipModel = vipInfo;
    }
    [self.tableView reloadData];
}

- (IBAction)buyAction:(UIButton *)sender {
    if(!self.selectVipModel){
        [[HUDHelper sharedInstance] tipMessage:@"请选择会员类型"];
        return;
    }
    EJSPayForVipViewController *payForVipVC = [[EJSPayForVipViewController alloc] initWithNibName:@"EJSPayForVipViewController" bundle:nil];
    payForVipVC.clientToken = self.vipInfoResponseModel.clientToken;
    payForVipVC.vipInfoModel = self.selectVipModel;
    [self.navigationController pushViewController:payForVipVC animated:YES];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"请选择VIP类型";
}


@end
